package cn.jas.common.jascommon.util;

import cn.jas.common.jascommon.annotation.CommonDept;
import cn.jas.common.jascommon.annotation.CommonDict;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 转换字典值
 * @author: chen_mx
 **/
@Component
public class CommonDictUtils {

    /**
     * 取值
     */
    public static String translateInstanceValue(String value) {
        return value.split(":")[1];
    }

    /**
     * 赋值
     */
    public static String translateInstanceCode(String value, String praent_code) {
        return praent_code + ":" + value;
    }


    /**
     * 单个实体进行转换字典值
     */
    @SuppressWarnings("all")
    public static <T> T translateObject(T t,Object  newVal) {
        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(CommonDict.class)) {
                //CommonDict  an= field.getAnnotation(CommonDict.class);
                try {
                    field.setAccessible(true);
                    Object val = field.get(t);
                    if (val instanceof String) {
                        String[] arr = ((String) val).split(":");
                        field.set(t, newVal);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            } else if (field.isAnnotationPresent(CommonDept.class)) {
                try {
                    field.setAccessible(true);
                    Object val = field.get(t);
                    if (val instanceof String) {
                        if (val != null) {
                            field.set(t, newVal);
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }

            }
        }
        return t;
    }

    /**
     * 集合实体进行转换字典值
     *
     * @auther chen_mx
     */
    @SuppressWarnings("all")
    public static <T> List<T> translateCollection(List<T> cls,Object  newVal) {
        List<T> list = new ArrayList<>();
        for (T obj : cls) {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (field.isAnnotationPresent(CommonDict.class)) {
                    //CommonDict  an= field.getAnnotation(CommonDict.class);
                    try {
                        field.setAccessible(true);
                        Object val = field.get(obj);
                        if (val instanceof String) {
                            if (String.valueOf(val).indexOf(":") != -1) {
                                String[] arr = ((String) val).split(":");
                                field.set(obj, newVal);
                            }
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                } else if (field.isAnnotationPresent(CommonDept.class)) {
                    try {
                        field.setAccessible(true);
                        Object val = field.get(obj);
                        if (val instanceof String) {
                            if (val != null) {
                                field.set(obj, newVal);
                            }
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }


                }
            }
            list.add(obj);
        }
        return list;
    }


    public static void main(String[] args) {

        // RiskDetails  ss=  translateObject(rf,"测试数据");
        //System.out.println(ss.toString());
    }
}
